package com.levy.contronller.lamdaTest;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

public class Person2Test {

    public static void main(String[] args) {

        Person2Test person2Test = new Person2Test();
        List<Person2> list = new ArrayList<>();
        list.add(new Person2("zhangsan", 18));
        list.add(new Person2("lisi", 30));
        list.add(new Person2("wangwu", 60));

        List<Person2> person2ByAge = person2Test.getPerson2ByAge(40, list);
        person2ByAge.stream().forEach(o -> System.out.println(o));

        System.out.println("-------------------------");

        List<Person2> person2 = person2Test.getPerson2(40, list, (a, l) ->
                l.stream().filter(o -> o.getAge() < a).collect(Collectors.toList())
        );
        person2.stream().forEach(o -> System.out.println(o));


    }


    public List<Person2> getPerson2ByAge(int age, List<Person2> list) {
        BiFunction<Integer, List<Person2>, List<Person2>> biFunction =
                (a, l) -> l.stream().filter(o -> o.getAge() > a).collect(Collectors.toList());
        List<Person2> apply = biFunction.apply(age, list);
        return apply;
    }


    public List<Person2> getPerson2(int age, List<Person2> list, BiFunction<Integer, List<Person2>, List<Person2>> biFunction) {
        return biFunction.apply(age, list);
    }


}


class Person2 {
    private String userName;

    private int age;

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("{");
        sb.append("\"userName\":\"")
                .append(userName).append('\"');
        sb.append(",\"age\":")
                .append(age);
        sb.append('}');
        return sb.toString();
    }

    public Person2(String userName, int age) {
        this.userName = userName;
        this.age = age;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
